//
//  main.cpp
//  Practice8
//
//  Created by 赵世杰 on 2020/7/4.
//  Copyright © 2020 zhaoshijie. All rights reserved.
//

#include <iostream>

using namespace std;


//  结构体

struct CandyBar {
    
    char name[20];
    float weight;
    int hot;
    
};

struct stringy {
    
    char *str;
    int ct;
    
};

struct debts {
    
    char name[50];
    double amount;
    
};


//  函数

void stringPrintf(string str , int count);

void candyBarContent(CandyBar &candyBar,const char * name ,const float weight ,const int hot);

void candyBarPrintf(const CandyBar &candyBar);

void letterChange(string str);

void set(stringy &gy ,char test[]);

void show(const stringy gy);

void show(const stringy gy ,const int index);

void show(const char *ch);

void show(const char *ch , const int index);


template <class T>

T max5(T number[]);


template <class T>

T maxn(T number[],int count);

template <> char * maxn(char * number[],int count);


template <typename T>

T SumArray(T arr[],int n);

template <typename T>

T SumArray(T *arr[],int n);




int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    
    
    /*
    //  1、
    
    stringPrintf("stringPrintf",10);
    */
    
    
    /*
    // 2、
    
    CandyBar candyBar;
    char name[20] = "Millennium Munch";
    float weight = 2.85;
    int hot = 350;
    
    candyBarContent(candyBar, name, weight, hot);
    candyBarPrintf(candyBar);
     */
    
    
    /*
    //  3、
    cout << "Enter a string (q to quit): ";
    
    string str ;
    
//    cin >> str;
    getline(cin,str);
    
    while (str != "q") {
        
        letterChange(str);
        
        
        
        cout << "Next string (q to quit): ";
        
//        cin >> str;
        getline(cin,str);
        
    }
     */
    
    
    /*
    //  4、
    stringy beany;
    
    char testing[] = "Reality isn't what it user to be. ";
    
    set(beany,testing);
    
    show(beany);
    show(beany, 2);
    
    testing[0] = 'D';
    testing[1] = 'u';
    
    show(testing);
    show(testing,3);
    */
    

    /*
    int number[] = {3,51,352,2,1};
    
    int maxValue = max5(number);
    
    cout << "最大值：" << maxValue << endl;
    
    
    double numer1[] = {2.41,349.1 ,331.845, 2344.3,3};
    
    double maxValue1 = max5(numer1);
    
    cout << "最大值：" << maxValue1 << endl;
    */

    
    /*
    int number[] = {3,51,352,2,1,5};
    
    int maxValue = maxn(number, sizeof(number)/sizeof(number[0]));
    
    cout << "最大值：" << maxValue << endl;
    
    double number1[] = {3.24,484.2,34.2,5};
    
    double maxValue1 = maxn(number1, sizeof(number1)/sizeof(number1[0]));
    
    cout << "最大值：" << maxValue1 << endl;

//    char *add = maxn(&number1,sizeof(number)/sizeof(number[0]));
    

    char* arr3[5]=
    {
        "hello",
        "i am a human",
        "i like sport",
        "i am 12 years old",
        "how about you"
    };
    cout<<"最长字符串："<<maxn(arr3,5)<<endl;
     */
    
    
    int things[6] = {13,31,103,301,310,130};
    
    struct debts mr_E[3] =
    {
        {"Ima Wolfe",2400},
        {"Ura Foxe",1300},
        {"Iby Stout",1800}
    };
    
    double *pd[3];
    
    for (int i=0; i<3; i++)
    {
        pd[i] = &mr_E[i].amount;
    }
    
    int value =  SumArray(things, sizeof(things)/sizeof(things[0]));
    
    cout << "Things sum : " << value << endl;
    
    
    double value1 =  SumArray(pd, sizeof(pd)/sizeof(pd[0]));
    
    cout << "pd sum : " << value1 << endl;
    
    
    return 0;
}

template <typename T>

T SumArray(T arr[],int n)
{
    double sum = 0;
    
    for (int i=0; i<n; i++)
    {
        sum += arr[i];
    }
    
    return sum;
    
}


template <typename T>

T SumArray(T *arr[],int n)
{
    double sum = 0;
    
    for (int i=0; i<n; i++)
    {
        sum += *(arr[i]);
    }
    
    return sum;
}


template <class T>

T maxn(T number[],int count)
{
    T num = number[0];
    
    for (int i=0; i<5; i++) {
        
        T value = number[i];
        
        if(value > num)
        {
            num = value;
        }
    }
    
    return num;
}

template <> char * maxn(char * number[],int count)
{
    char * ch = *number;
    
    for (int i=0; i<5; i++) {
        
        char * value = *(number +i);
        
        if(strlen(ch) < strlen(value))
        {
            ch = value;
        }
    }
    
    return ch;
}


template <class T>

T max5(T number[])
{
    T num = number[0];
    
    for (int i=0; i<5; i++) {
        
        T value = number[i];
        
        if(value > num)
        {
            num = value;
        }
    }
    
    return num;
    
}

void show(const stringy gy)
{
//    cout << "gy: " << gy.str << " length: " << gy.ct << endl;
    
    if(gy.ct>=1)
    {
        cout << "gy.sty 的第1位置：" << gy.str[0] << endl;
    }
    
}

void show(const stringy gy ,const int index)
{
    if(gy.ct>=index)
    {
        cout << "gy.sty 的第" << index << "位置：" ;
        
        for (int i=0; i<index; i++) {
            
            cout << gy.str[i] ;
            
        }
        
        cout << endl;
        
    }
}


void show(const char *ch)
{
    if(strlen(ch) >= 1)
    {
        cout << "ch 的第1位置：" << ch[0] << endl;
    }
}


void show(const char *ch , const int index)
{
    if(strlen(ch) >= 1)
    {
        cout << "ch 的第" << index << "位置：" ;
        
        for (int i=0; i<index; i++) {
            
            cout << ch[i] ;
            
        }
        
        cout << endl;
        
    }
}


void set(stringy &gy ,char *test)
{
    
    gy.str = test;
    
    gy.ct = (int)strlen(test);
    
}



void letterChange(const string str)
{
    for (int i=0; i<str.length(); i++)
    {
        char ch = str[i];
        
        ch = toupper(ch);
    
        cout << ch;
        
    }
    
    
    cout << endl;
    
}



void candyBarContent(CandyBar &candyBar,const char * name ,const float weight ,const int hot)
{
    stpcpy(candyBar.name, name);
    candyBar.weight = weight;
    candyBar.hot = hot;
    
}


void candyBarPrintf(const CandyBar &candyBar)
{
    
    cout << candyBar.name << " " << candyBar.weight << " " << candyBar.hot << endl;
    
}



void stringPrintf(string str , int count)
{
    if(count > 0)
    {
        for (int i=0; i<count; i++)
        {
            cout << str << endl;
            
        }
    }
    else
    {
        cout << str << endl;
    }
}
